package com.glory.hikvisionmobile.adapter;

import android.content.Context;
import android.widget.CheckBox;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.bean.LotScrap.ScrapListBean;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class LotScrapCancelListAdapter extends BaseQuickAdapter<ScrapListBean, BaseViewHolder> {

    private List<ScrapListBean> mData;
    private String mScrapCode;
    private Context mContext;

    public Context getContext() {
        return mContext;
    }

    public void setContext(Context mContext) {
        this.mContext = mContext;
    }

    public LotScrapCancelListAdapter(@Nullable List<ScrapListBean> data) {
        super(R.layout.adapter_param_list,data);
        mData = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, ScrapListBean item) {
        helper.setText(R.id.tvItem1,item.getComponentId());
        helper.setText(R.id.tvItem2,item.getPosition());
        helper.setText(R.id.tvItem3,item.getActionCode());
        helper.setText(R.id.tvItem4,item.getUnActionCode());
        CheckBox checkBox = helper.getView(R.id.pauseCb);
        helper.setGone(R.id.cbLayout,true);
        helper.setGone(R.id.tvItem5,false);
        if (item.isCheck()) {
            checkBox.setChecked(true);
            mData.add(item);
        }else {
            checkBox.setChecked(false);
            if (mData.contains(item)){
                mData.remove(item);
            }
        }

        checkBox.setOnClickListener(view -> {
            if (getScrapCode().length() == 0){
                FreeApi_ToastUtils.showFreeToast("请选择取消报废码",mContext,false,0);
                checkBox.setChecked(false);
                return;
            }
            if (checkBox.isChecked()) {
                item.setCheck(true);
                item.setUnActionCode(mScrapCode);
            }else {
                item.setCheck(false);
                item.setUnActionCode("");
            }

            notifyItemChanged(helper.getAdapterPosition());
        });

    }

    public List<ScrapListBean> getSelectorData(){
        return mData;
    }


    public String getScrapCode() {
        return mScrapCode == null ? "" : mScrapCode;
    }

    public void setScrapCode(String mScrapCode) {
        this.mScrapCode = mScrapCode;
    }

}
